<?php

namespace app\admin\controller\kuyou;

use app\common\controller\Backend;
use Exception;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Session;

/**
 * 生产成本
 *
 * @icon fa fa-circle-o
 */
class Acceptances extends Backend
{
    /**
     * Procedure模型对象
     * @var \app\admin\model\kuyou\Acceptance
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\kuyou\Acceptance;
        $setData = (new Goods())->getSetData();
        $this->assign('set', $setData['setList']);
        $this->assignconfig('set', $setData['setList']);
        $this->assign('nowmenu', $setData['nowmenu']);

        $stores_list = Db::name('kuyou_stores')->where('shop_id', $this->shop_id)->where(['status' => '1'])->select();
        $this->view->assign("storesList", $stores_list);
        $this->assignconfig("storesList", $stores_list);
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index($ids = null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where('acceptance.shop_id', $this->shop_id)
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $list_k => $list_v) {
                $list[$list_k]['workshop_id'] = Db::name('kuyou_workshop')->where('shop_id', $this->shop_id)->where(['id' => $list_v['workshop_id']])->column('name');
                if ($list_v['code_type'] == 1) {
                    //加工单
                    $production_id = Db::name('kuyou_production')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['chase_code']])->value('id');
                    $goods_list = Db::name('kuyou_production_info')->where('shop_id', $this->shop_id)->where(['production_id' => $production_id, 'type' => 2])->field('goods_name,number')->select();
                    $list[$list_k]['outsourcing_free'] = 0;
                    $list[$list_k]['hour_free'] = 0;
                    $list[$list_k]['wages_free'] = 0;
                    $production = Db::name('kuyou_production')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['chase_code']])->find();
                    $production_info = Db::name('kuyou_production_info')->where('shop_id', $this->shop_id)->where(['production_id' => $production['id'], 'type' => '1'])->select();

                    $acceptance_list = Db::name('kuyou_acceptance_info')->where('shop_id', $this->shop_id)->where(['production_id' => $list_v['id']])->column('number', 'goods_code');
                    $hour_free = 0;
                    $wages_free = 0;
                    if ($production) {
                        $production_id = Db::name('kuyou_plan')->where('shop_id', $this->shop_id)->where(['purchase_code' => $production['plan_code']])->value('id');
                        $goods_list = Db::name('kuyou_plan_info')->where('shop_id', $this->shop_id)->where(['plan_id' => $production_id])->field('goods_name,number,goods_code')->select();
                        foreach ($goods_list as $goods_list_k => $goods_list_v) {
                            $bom_id = Db::name('kuyou_bom')->where('shop_id', $this->shop_id)->where(['goods_code' => $goods_list_v['goods_code']])->value('id');
                            $procedure = Db::name('kuyou_bom_procedure')->where('shop_id', $this->shop_id)->where(['bom_id' => $bom_id])->select();
                            $this_wages = Db::name('kuyou_bom')->where('shop_id', $this->shop_id)->where(['goods_code' => $goods_list_v['goods_code']])->value('wages');
                            if (isset($acceptance_list[$goods_list_v['goods_code']])) {
                                $wages_free += $this_wages * $acceptance_list[$goods_list_v['goods_code']];
                                foreach ($procedure as $procedure_k => $procedure_v) {
                                    $hour_free += ($procedure_v['wages'] * $procedure_v['hours']) * $acceptance_list[$goods_list_v['goods_code']];
                                }
                            }
                        }
                    } else {
                        $goods_list = [];
                    }
                    $list[$list_k]['hour_free'] = $hour_free;
                    $list[$list_k]['wages_free'] = $wages_free;

                    $material = Db::name('kuyou_material')->where('shop_id', $this->shop_id)->where(['production_code' => $list_v['chase_code']])->value('id');
                    $material_info = Db::name('kuyou_material_info')->where('shop_id', $this->shop_id)->where(['production_id' => $material])->select();
                } else {
                    //委外加工单
                    $plan = Db::name('kuyou_outsourcing')->where('shop_id', $this->shop_id)->where(['purchase_code' => $list_v['chase_code']])->find();
                    $production_info = Db::name('kuyou_outsourcing_info')->where('shop_id', $this->shop_id)->where(['production_id' => $plan['id']])->select();
                    $list[$list_k]['outsourcing_free'] = $plan['free_money'];
                    $acceptance_list = Db::name('kuyou_acceptance_info')->where('shop_id', $this->shop_id)->where(['production_id' => $list_v['id']])->column('number', 'goods_code');
                    $hour_free = 0;
                    $wages_free = 0;
                    if ($plan) {
                        $production_id = Db::name('kuyou_plan')->where('shop_id', $this->shop_id)->where(['purchase_code' => $plan['plan_code']])->value('id');
                        $goods_list = Db::name('kuyou_plan_info')->where('shop_id', $this->shop_id)->where(['plan_id' => $production_id])->field('goods_name,number,goods_code')->select();
                        foreach ($goods_list as $goods_list_k => $goods_list_v) {
                            $bom_id = Db::name('kuyou_bom')->where('shop_id', $this->shop_id)->where(['goods_code' => $goods_list_v['goods_code']])->value('id');
                            $procedure = Db::name('kuyou_bom_procedure')->where('shop_id', $this->shop_id)->where(['bom_id' => $bom_id])->select();
                            $this_wages = Db::name('kuyou_bom')->where('shop_id', $this->shop_id)->where(['goods_code' => $goods_list_v['goods_code']])->value('wages');
                            $wages_free += $this_wages * $acceptance_list[$goods_list_v['goods_code']];
                            foreach ($procedure as $procedure_k => $procedure_v) {
                                $hour_free += ($procedure_v['wages'] * $procedure_v['hours']) * $acceptance_list[$goods_list_v['goods_code']];
                            }
                        }
                    } else {
                        $goods_list = [];
                    }
                    $list[$list_k]['hour_free'] = $hour_free;
                    $list[$list_k]['wages_free'] = $wages_free;

                    $material = Db::name('kuyou_material')->where('shop_id', $this->shop_id)->where(['plan_code' => $list_v['chase_code']])->value('id');
                    $material_info = Db::name('kuyou_material_info')->where('shop_id', $this->shop_id)->where(['production_id' => $material])->select();
                }
                $str = '';
                foreach ($goods_list as $goods_list_k => $goods_list_v) {
                    $str .= $goods_list_v['goods_name'] . '*' . $goods_list_v['number'] . ' ';
                }
                $list[$list_k]['goods_info'] = $str;

                $production_free = 0;
                foreach ($production_info as $production_info_k => $production_info_v) {
                    $production_free += $production_info_v['money'];
                }
                $list[$list_k]['production_free'] = $production_free;

                $material_free = 0;
                foreach ($material_info as $material_info_k => $material_info_v) {
                    $material_free += $material_info_v['money'];
                }
                $list[$list_k]['material_free'] = $material_free;

                $cost = $list[$list_k]['outsourcing_free'] + $list[$list_k]['hour_free'] + $list[$list_k]['wages_free'] + $list[$list_k]['production_free'] - $material_free;
                $list[$list_k]['cost_free'] = $cost;
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        $this->assignconfig('ids', $ids);
        return $this->view->fetch();
    }

}
